package view;

import java.awt.Container;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import controller.UI;

/**
 * Class is responsible for the logger table building.
 */
public class LoggerTable extends JTable
{
	/* Current logger table */
	public static LoggerTable currentLoggerTable = null;
	
	/* Column names */
	private final static String columnNames[] = 
		{"Time", "Direction", "Source IP", "Destination IP", "Source port", "Destination port", "Protocol", 
		 "Rule port", "Rule protocol", "Status" };
	
	/* Table data */
	public static ArrayList loggerData = new ArrayList();
		
	/**
	 * C'tor
	 * Create table with scroll.
	 * Size is defined hard coded.
	 */
	public LoggerTable()
	{
		super(Settings.windowDepth, columnNames.length);
		
		currentLoggerTable = this;
		loggerData = UI.getLoggerSessionsReports(Settings.DEFAULT_LOGGER_WINDOW_DEPTH);
	    setAutoResizeMode (JTable.AUTO_RESIZE_OFF);

		build();
	}
	
	/**
	 * Create table header.
	 */
	public JTableHeader getLoggerTableHeader()
	{
		TableColumn column = null;
		for (int i = 0; i < columnNames.length; i++)
		{
		    column = getColumnModel().getColumn(i);
		    column.setHeaderValue(columnNames[i]);
		}
		
		return getTableHeader();	
	}
	
	/**
	 * Cancel cell editing for user.
	 */
	public boolean isCellEditable(int row, int column) 
	{
		return false;
	}

	public boolean getScrollableTracksViewportHeight() {

		if (autoResizeMode == AUTO_RESIZE_OFF)
		{
			Container parent = getParent();
			if (parent instanceof JViewport)
			{
				return (parent.getSize().getHeight() > getPreferredSize().getHeight());
			}
		}
		return super.getScrollableTracksViewportHeight();
	}

	/**
	 * Fill the table.
	 */
	public void build()
	{
		getLoggerData();
		String[] line = null;
		
		if(loggerData != null)
		{
			/* Add new rows if it's needed */
			int diff = loggerData.size() - getModel().getRowCount();
			
			/* Clear the previous data */
			((DefaultTableModel)getModel()).setRowCount(0);
			if(diff > 0)
			{
				((DefaultTableModel)getModel()).setRowCount(loggerData.size());
			}
			else
			{
				((DefaultTableModel)getModel()).setRowCount(Settings.DEFAULT_LOGGER_WINDOW_DEPTH);
			}
			
			/* Fill the data */
			for(int i = 0; i < loggerData.size(); i++)
			{
				line = (String[]) loggerData.get(i);
				for(int j = 0; j < line.length; j++)
				{
					this.setValueAt(line[j], i, j);
				}
			}			
		}
		else /*if(loggerData != null)*/
		{
			/* Clear the previous data */			
			((DefaultTableModel)getModel()).setRowCount(0);
			((DefaultTableModel)getModel()).setRowCount(Settings.DEFAULT_LOGGER_WINDOW_DEPTH);
		}
	}
	
	/**
	 * Take the data from UI.
	 */
	private void getLoggerData()
	{
		
		if(Settings.filterOptionNeeded)
		{
			if(Settings.filterParameterNeeded)
			{
				LoggerTable.loggerData = 
					UI.getLoggerSessionsReports(Settings.historyDepth, Settings.filterOption, Settings.filterParameter);				
			}
			else
			{
				LoggerTable.loggerData = 
					UI.getLoggerSessionsReports(Settings.historyDepth, Settings.filterOption);			
			}
		}
		else
		{
			LoggerTable.loggerData = UI.getLoggerSessionsReports(Settings.historyDepth);
		}
	}
}
